﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Water.Architecture2.IDAL;
using System.Data;
using System.Reflection;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Web;
using System.Collections;

namespace Water.Web.Controls
{
    public class ObjectHierarchicalDataSourceView : HierarchicalDataSourceView
    {
        #region private field

        private object typeInstance;
        private MethodInfo method;
        private string parentPath;
        private string dataIDField;
        private string dataParentIDField;
        private string isLeafField;
        private string rootParentID;
        private DataSet childItems;

        private ParameterCollection _selectParameters;
        private ParameterCollection _rootParameters;

        private HttpContext _context;
        private ObjectHierarchicalDataSource _owner;
 




        #endregion

        #region Constructors
        public ObjectHierarchicalDataSourceView(string dataIDField, string dataParentIDField,string isLeafField, string parentPath, string rootParentID, object typeInstance, MethodInfo method, HttpContext context, ObjectHierarchicalDataSource owner, ParameterCollection selectParameters,ParameterCollection rootParameters )
        {
            this.parentPath = parentPath;
            this.typeInstance = typeInstance;
            this.method = method;
            this.dataIDField = dataIDField;
            this.dataParentIDField = dataParentIDField;
            this.rootParentID = rootParentID;

            this._context = context;
            this._owner = owner;
            this._selectParameters = selectParameters;
            this._rootParameters = rootParameters;
            this.isLeafField = isLeafField;


            if (parentPath == null || (parentPath != null && (parentPath.Trim().Equals("/") || parentPath.Trim().Equals(""))))//root
            {
                if (this.RootParameters.Count > 0)
                {
                    WhereFields wheres = new WhereFields();
                    ProcessSelectParameters(wheres);
                    ProcessRootParameters(wheres);
                    WhereFields[] parameters = new WhereFields[] { wheres };
                    childItems = (DataSet)method.Invoke(this.typeInstance, parameters);
                }
                else
                {
                    WhereFields wheres = new WhereFields();
                    wheres.Add(this.dataIDField, OperatorType.Equals, this.rootParentID);
                    ProcessSelectParameters(wheres);
                    WhereFields[] parameters = new WhereFields[] { wheres };

                    childItems = (DataSet)method.Invoke(this.typeInstance, parameters);
                    if (childItems.Tables[0].DefaultView.Count == 0)
                    {
                        WhereFields wheres1 = new WhereFields();
                        wheres1.Add(this.dataParentIDField, OperatorType.Equals, this.rootParentID);
                        ProcessSelectParameters(wheres1);
                        WhereFields[] parameters1 = new WhereFields[] { wheres1 };
                        childItems = (DataSet)method.Invoke(this.typeInstance, parameters1);
                    }
                }
            }
            else
            {
                int _index = this.parentPath.LastIndexOf('/');
                if (_index > -1)
                {
                    string _parentParentPath = this.parentPath.Substring(_index + 1);

                    WhereFields wheres = new WhereFields();
                    wheres.Add(this.dataParentIDField, OperatorType.Equals, _parentParentPath);
                    ProcessSelectParameters(wheres);
                    WhereFields[] parameters = new WhereFields[] { wheres };
                    childItems = (DataSet)method.Invoke(this.typeInstance, parameters);
                }
            }
        }

        
        #endregion

        #region ovveride
        public override IHierarchicalEnumerable Select()
        {
            return new ObjectHierarchicalDataCollection(dataIDField, dataParentIDField,isLeafField, parentPath, childItems, typeInstance, method,_context, _owner, _selectParameters,_rootParameters);
        }
        #endregion


        public ParameterCollection SelectParameters
        {
            get
            {
                if (this._selectParameters == null)
                    this._selectParameters = new ParameterCollection();
                return this._selectParameters;
            }
            set
            {
                this._selectParameters=value;
            }
        }


        public ParameterCollection RootParameters
        {
            get
            {
                if (this._rootParameters == null)
                    this._rootParameters = new ParameterCollection();
                return this._rootParameters;
            }
            set
            {
                this._rootParameters = value;
            }
        }


        #region help methods
        private void ProcessRootParameters(WhereFields wheres)
        {
            foreach (DictionaryEntry entry in this.RootParameters.GetValues(this._context, this._owner))
            {
                wheres.Add(entry.Key.ToString(), OperatorType.Equals, entry.Value);
            }
        }
        private void ProcessSelectParameters(WhereFields wheres)
        {
            foreach (DictionaryEntry entry in this.SelectParameters.GetValues(this._context, this._owner))
            {
                wheres.Add(entry.Key.ToString(), OperatorType.Equals, entry.Value);
            }
        }
        #endregion
    }
}
